package com.ww.springboot.boot.algorithm.leetcode1;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-03-11 10:08
 */
public class AI54螺旋矩阵 {

    public static void main(String[] args) {
        int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        System.out.println(spiralOrder(matrix));
    }

    public static List<Integer> spiralOrder(int[][] matrix) {
        int xLen = matrix.length;
        int yLen = matrix[0].length;
        int total = xLen * yLen;
        int count = 0;
        List<Integer> list = new ArrayList<>();
        int x = 0, y = 0;
        int depth = 1;
        int face = 0;
        list.add(matrix[x][y]);
        while (count < total - 1) {
            if (y < yLen - depth && face == 0) {
                y++;
            } else if (x < xLen - depth && face != 3) {
                x++;
                face = 1;
            } else if (y > depth - 1) {
                y--;
                face = 2;
            } else if (x > depth - 1 + 1) {
                face = 3;
                x--;
            } else {
                x = depth;
                y = depth;
                depth++;
                face = 0;
            }
            list.add(matrix[x][y]);
            count++;
        }
        return list;
    }
}
